<template>
  <div class="form-add">
    <div class="form-container">
      <div class="form-header">
        <el-button @click="cancel" size="small">返回</el-button>
        <el-button type="primary" @click="sure" size="small">保存</el-button>
      </div>
      <div class="form-content">
        <el-form ref="form" :model="form" label-width="100px">
          <el-form-item label="管理员">
            <el-input v-model="form.AccountName"></el-input>
          </el-form-item>
          <el-form-item label="密码">
            <el-input v-model="form.password"></el-input>
          </el-form-item>
          <el-form-item label="确认密码">
            <el-input v-model="form.confimrPassword"></el-input>
          </el-form-item>
          <el-form-item label="姓名">
            <el-input v-model="form.CallName"></el-input>
          </el-form-item>
          <el-form-item label="邮箱">
            <el-input v-model="form.Email"></el-input>
          </el-form-item>
          <el-form-item label="电话">
            <el-input v-model="form.Telephone"></el-input>
          </el-form-item>
          <el-form-item label="权限组">
            <el-select v-model="form.PurviewGroupIds" multiple>
              <el-option
                v-for="item in groupList"
                :key="item.PurviewGroupId"
                :value="item.PurviewGroupId"
                :label="item.GroupName"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="状态">
            <el-radio-group v-model="form.Status">
              <el-radio :label="1">启用</el-radio>
              <el-radio :label="0">停用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
import { purviewGroup } from '../../models/fake-data';
export default {
  name: 'Edit',
  props: ['data'],
  data() {
    return {
      form: {
        Status: 1
      },
      groupList: [],
    }
  },
  mounted() {
    this.purviewgroupList();
    this.info();
  },
  methods: {
    sure: function () {
      let url = 'add';
      if (this.data && this.data.SysUserId) {
        url = 'update'
      }
      let req = {
        AccountName: this.form.AccountName,
        CallName: this.form.CallName,
        Telephone: this.form.Telephone || '',
        Email: this.form.Email,
        PurviewGroupIds: this.form.PurviewGroupIds && this.form.PurviewGroupIds.sort((a, b) => a - b),
        Status: this.form.Status,
      };
      if (this.data && this.data.SysUserId) {
        req.SysUserId = this.data.SysUserId;
      }
      if (this.form.password || url == 'add') {
        req.AccountPassword = this.form.password;
      }
      this.$message.success('保存成功');
      this.$emit('sure') // 保存回调父组件方法
    },

    cancel: function () {
      this.$emit('cancel') // 返回回调父组件方法
    },

    info: function () {
      if (!this.data || !this.data.SysUserId) {
        return;
      }
      this.form = this.data;
    },

    purviewgroupList: function () {
      this.groupList = purviewGroup;
    }
  }
}
</script>
